﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using KVB.Common;

namespace Customer_Profile.eCard.DBDao
{
    public class TransferDao
    {
        public static bool AddTransfer(string deal_no
            , string card_no
            , string account_name
            , string bank_name
            , string account_no
            , string transfer_type
            , decimal transfer_amount
            , string to_account
            , string transfer_date
            , string remark
            , string userid)          //添加一个客户转账信息
        {
            SqlConnection conn = null;
            try
            {
                DB.Connection(ref conn);
                SqlParameter[] parameters ={ new SqlParameter("@deal_no ", SqlDbType.VarChar,50),
                                             new SqlParameter("@card_no ", SqlDbType.VarChar,50),
                                             new SqlParameter("@account_name ",SqlDbType.NVarChar,50),
                                             new SqlParameter("@bank_name ",SqlDbType.NVarChar,100),
                                             new SqlParameter("@account_no ",SqlDbType.VarChar,50 ),
                                             new SqlParameter("@transfer_type ",SqlDbType.VarChar,50),
                                             new SqlParameter("@transfer_amount ",SqlDbType.Float),
                                             new SqlParameter("@to_account ",SqlDbType.VarChar,50),
                                             new SqlParameter("@transfer_date ",SqlDbType.VarChar,50),
                                             new SqlParameter("@remark ",SqlDbType.NVarChar,250),
                                             new SqlParameter("@userid ",SqlDbType.NVarChar,250),
                                           };

                parameters[0].Value = deal_no;
                parameters[1].Value = card_no;
                parameters[2].Value = account_name;
                parameters[3].Value = bank_name;
                parameters[4].Value = account_no;
                parameters[5].Value = transfer_type;
                parameters[6].Value = transfer_amount;
                parameters[7].Value = to_account;
                parameters[8].Value = transfer_date;
                parameters[9].Value = remark;
                parameters[10].Value = userid;
                DB.ExecuteNonQuery(conn, CommandType.StoredProcedure, "eCard_transfer_insert", parameters);
                return true;
            }
            catch (Exception e)
            {
                KVB.Common.Log.WriteDefaultLog("TransferDao.AddTransfer", e.Message);
                return false;
            }
            finally
            {
                DB.ConnDispose(ref conn);
            }
        }

        public static bool UpdateTransfer(string transfer_id
            ,string card_no
            ,string account_name
           , string bank_name
           , string account_no
           , string transfer_type
           , string transfer_amount
           , string to_account
           , string transfer_date
           , string remark)          //修改一个客户转账信息
        {
            SqlConnection conn = null;
            try
            {
                DB.Connection(ref conn);
                SqlParameter[] parameters ={ new SqlParameter("@transfer_id", SqlDbType.VarChar,50),      
                                             new SqlParameter("@card_no", SqlDbType.VarChar,50),
                                             new SqlParameter("@account_name",SqlDbType.NVarChar,50),
                                             new SqlParameter("@bank_name",SqlDbType.NVarChar,100),
                                             new SqlParameter("@account_no",SqlDbType.VarChar,50 ),
                                             new SqlParameter("@transfer_type",SqlDbType.VarChar,50),
                                             new SqlParameter("@transfer_amount",SqlDbType.Decimal),
                                             new SqlParameter("@to_account",SqlDbType.NVarChar,50),
                                             new SqlParameter("@transfer_date",SqlDbType.VarChar,50),
                                             new SqlParameter("@remark",SqlDbType.NVarChar,250)};

                parameters[0].Value = transfer_id;
                parameters[1].Value = card_no;    
                parameters[2].Value = account_name;
                parameters[3].Value = bank_name;
                parameters[4].Value = account_no;
                parameters[5].Value = transfer_type;
                parameters[6].Value = transfer_amount;
                parameters[7].Value = to_account;
                parameters[8].Value = transfer_date;
                parameters[9].Value = remark;
                DB.ExecuteNonQuery(conn, CommandType.StoredProcedure, "eCard_transfer_update", parameters);
                return true;
            }
            catch (Exception e)
            {
                KVB.Common.Log.WriteDefaultLog("TransferDao.UpdateTransfer", e.Message);
                return false;
            }
            finally
            {
                DB.ConnDispose(ref conn);
            }
        }

        public static bool deleteTransfer(string transfer_id)          //修改一个客户转账信息
        {
            SqlConnection conn = null;
            try
            {
                DB.Connection(ref conn);
                SqlParameter[] parameters = { new SqlParameter("@transfer_id", SqlDbType.VarChar, 50) };
                parameters[0].Value = transfer_id;
                DB.ExecuteNonQuery(conn, CommandType.StoredProcedure, "eCard_transfer_delete", parameters);
                return true;
            }
            catch (Exception e)
            {
                KVB.Common.Log.WriteDefaultLog("TransferDao.deleteTransfer", e.Message);
                return false;
            }
            finally
            {
                DB.ConnDispose(ref conn);
            }
        }

        public static DataSet getTransfers(string deal_no)          //搜索 Transfer by cardno
        {
            DataSet ds = null;
            SqlConnection conn = null;
            try
            {
                DB.Connection(ref conn);
                SqlParameter[] parameters ={ new SqlParameter("@deal_no ", SqlDbType.VarChar,50)};

                parameters[0].Value = deal_no;

                ds = DB.ExecuteDataset(conn, CommandType.StoredProcedure, "eCard_transfers_bycardno",parameters);
            }
            catch (Exception e)
            {
                KVB.Common.Log.WriteDefaultLog("TransferDao.getTransfers", e.Message);
            }
            finally
            {
                DB.ConnDispose(ref conn);
            }
            return ds;
        }
        
        public static DataSet test()          //搜索 测试用
        {
            DataSet ds = null;
            SqlConnection conn = null;
            try
            {
                string strSQL = "select transfer_id,account_name,bank_name,account_no,transfer_type,transfer_amount,to_account,remark from bank_transfer";
                DB.Connection(ref conn);
                ds = DB.ExecuteDataset(conn, CommandType.Text, strSQL);
            }
            catch (Exception e)
            {
                KVB.Common.Log.WriteDefaultLog("TransferDao.test", e.Message);
            }
            finally
            {
                DB.ConnDispose(ref conn);
            }
            return ds;
        }

        public static void testadd(string cardno,string bankname)          //搜索 测试用
        {
            
            SqlConnection conn = null;
            try
            {
                string strSQL = "insert into bank_transfer(card_no,bank_name) values('" + cardno + "','" + bankname + "')";
                DB.Connection(ref conn);
                 DB.ExecuteNonQuery(conn, CommandType.Text, strSQL);
            }
            catch (Exception e)
            {
                KVB.Common.Log.WriteDefaultLog("TransferDao.test", e.Message);
            }
            finally
            {
                DB.ConnDispose(ref conn);
            }
            
        }

        public static void testupdate(string cardno, string bankname,string transfer_id)          //搜索 测试用
        {

            SqlConnection conn = null;
            try
            {
                string strSQL = "update bank_transfer set card_no ='" +cardno+ "', bank_name ='" +bankname+ "' where transfer_id ='"+transfer_id+"'";
                DB.Connection(ref conn);
                DB.ExecuteNonQuery(conn, CommandType.Text, strSQL);
            }
            catch (Exception e)
            {
                KVB.Common.Log.WriteDefaultLog("TransferDao.test", e.Message);
            }
            finally
            {
                DB.ConnDispose(ref conn);
            }

        }
    }
}